package org.darcy.controller.business;

import java.util.List;

import org.darcy.entity.business.BusinessContract;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.framework.model.R;
import org.darcy.service.business.BusinessClientService;
import org.darcy.service.business.BusinessContractService;
import org.darcy.vo.project.ProjectList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "BusinessContract接口：合同登记")
@RestController
@RequestMapping(value = "/BusinessContract")
@Slf4j
public class BusinessContractController {

	private final String info_prefix = "[合同登记模块]";

	@Autowired
	private BusinessContractService businessContractService;

	@Autowired
	private BusinessClientService BusinessClientService;

	@Operation(summary = "所有合同列表")
	@GetMapping(value = "getAll")
	public R getAll(String keyword) {
		log.debug("{}-所有合同列表", info_prefix);
		List<BusinessContract> data = businessContractService.getAll(keyword);
		return R.ok().data(data);
	}

	@Operation(summary = "根据合同id获取客户开票资料")
	@GetMapping(value = "getBank")
	public R getBank(Integer id) {
		BusinessContract BusinessContract = businessContractService.getById(id);
		return R.ok().data(BusinessClientService.getDetail(BusinessContract.getClientId()).getBankInfoList());
	}

	@Operation(summary = "合同登记分页列表")
	@PostMapping(value = "list")
	public R list(@RequestBody PageParam<ProjectList> param) {
		log.debug("{}-分页列表数据", info_prefix);
		PageData<BusinessContract> data = businessContractService.page(param);
		return R.ok().data(data);
	}

	@Operation(summary = "合同登记详情")
	@GetMapping("/view")
	public R view(Integer id) {
		log.debug("{}-查看合同登记信息", info_prefix);
		BusinessContract data = businessContractService.getDetail(id);
		return R.ok().data(data);
	}

	@Operation(summary = "新增合同登记")
	@PostMapping("add")
	public R add(@RequestBody BusinessContract record) {
		log.debug("{}-新增合同登记: {}", info_prefix, record);
		businessContractService.insert(record);
		return R.ok("新增成功").data(record);
	}

	@Operation(summary = "根据ID删除合同登记")
	@PostMapping("/del/{id}")
	public R del(@PathVariable Integer id) {
		log.debug("{}-根据ID删除", info_prefix, id);
		businessContractService.delete(id);
		return R.ok("删除成功");
	}

	@Operation(summary = "更新合同登记")
	@PostMapping("update")
	public R update(@RequestBody BusinessContract record) {
		log.debug("{}-保存信息: {}", info_prefix, record);
		businessContractService.updateFlow(record);
		return R.ok("更新成功");
	}

	@Operation(summary = "更新合同确认")
	@PostMapping("updateConfirm")
	public R updateConfirm(@RequestBody BusinessContract record) {
		log.debug("{}-保存信息: {}", info_prefix, record);
		businessContractService.updateConfirm(record);
		return R.ok("更新成功");
	}

	@Operation(summary = "查看合同登记属性")
	@GetMapping("/model")
	public BusinessContract model() {
		return null;
	}

	@Operation(summary = "项目详情")
	@GetMapping("/projectView/{id}")
	public R projectView(@PathVariable Integer id) {
		log.debug("{}-查看项目详情", info_prefix);
		BusinessContract data = businessContractService.getProjectData(id);
		return R.ok().data(data);
	}

}
